//
//  JSONToModelVC.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/10/26.
//

import Cocoa

class JSONToModelVC: NSViewController {

    lazy var viewModel = JSONToModelVM()
    @IBOutlet weak var typeSegment: NSSegmentedControl!
    @IBOutlet weak var languageSegment: NSSegmentedControl!
    @IBOutlet weak var prefixField: NSTextField!
    @IBOutlet weak var modelTextView: NSTextView!
    @IBOutlet weak var jsonTextView: NSTextView!
    @IBOutlet weak var modelCustomView: NSView!
    @IBOutlet weak var jsonCustomView: NSView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        jsonTextView.font = .systemFont(ofSize: 15)
        jsonTextView.isAutomaticQuoteSubstitutionEnabled = false
        modelTextView.isAutomaticQuoteSubstitutionEnabled = false
        
        modelCustomView.wantsLayer = true
        modelCustomView.layer?.cornerRadius = 15
        modelCustomView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.3).cgColor
        
        
        jsonCustomView.wantsLayer = true
        jsonCustomView.layer?.cornerRadius = 15
        jsonCustomView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.3).cgColor
        
        jsonTextView.delegate = self
        prefixField.delegate = self
        
        viewModel.contentChangeBlock = {[weak self] convertString in
            self?.refreshModelTextView(convertString)
        }
    }
    
    @IBAction func typeChangeAction(_ sender: NSSegmentedControl) {
        if sender.selectedSegment == 0 {
            viewModel.structureType = .class
        }else {
            viewModel.structureType = .`structOrInterface`
        }
    }
    
    @IBAction func languageChangeAction(_ sender: NSSegmentedControl) {
        if sender.selectedSegment == 0 {
            viewModel.languageType = .swift
            typeSegment.setLabel("Struct", forSegment: 1)
        }else if sender.selectedSegment == 1 {
            viewModel.languageType = .oc
            typeSegment.setLabel("Struct", forSegment: 1)
        }else{
            viewModel.languageType = .arkTS
            typeSegment.setLabel("Interface", forSegment: 1)
        }
        typeSegment.isEnabled = sender.selectedSegment != 1
    }
    
    
}

extension JSONToModelVC {
    func refreshModelTextView(_ content: String) {
        modelTextView.font = .systemFont(ofSize: 15)
        let attributedString = NSMutableAttributedString(string: content)
        attributedString.addAttribute(.font, value: NSFont.systemFont(ofSize: 15), range: NSMakeRange(0, content.count))
        if languageSegment.selectedSegment == 0 {
            let classRange = content.ranges(of: "class ") + content.ranges(of: "struct ") + content.ranges(of: "var ") + content.ranges(of: "import ") + content.ranges(of: " Any")
            attributedString.addAttribute(.foregroundColor, value: "D059D1".color(), ranges: classRange)
            let foundationRange = content.ranges(of: "NSObject ") + content.ranges(of: " Int?") + content.ranges(of: " String ") + content.ranges(of: " Double?") + content.ranges(of: " [Int]") + content.ranges(of: " [Double]")
            attributedString.addAttribute(.foregroundColor, value: "259027".color(), ranges: foundationRange)
            do {
                let propertyRangs = try viewModel.obtainPropertyNameRang(content)
                attributedString.addAttribute(.foregroundColor, value: "1732FF".color(), ranges: propertyRangs)
            }catch { }
            modelTextView.textStorage?.setAttributedString(attributedString)
        }else if languageSegment.selectedSegment == 2 {
            let classRange = content.ranges(of: "class ") + content.ranges(of: "interface ") + content.ranges(of: " unknown") + content.ranges(of: " null") + content.ranges(of: " boolean")
            attributedString.addAttribute(.foregroundColor, value: "D059D1".color(), ranges: classRange)
            let foundationRange = content.ranges(of: "number ") + content.ranges(of: " string ") + content.ranges(of: " number[]") + content.ranges(of: " string[]")
            attributedString.addAttribute(.foregroundColor, value: "259027".color(), ranges: foundationRange)
            do {
                let propertyRangs = try viewModel.obtainArkTSPropertyNameRang(content)
                attributedString.addAttribute(.foregroundColor, value: "1732FF".color(), ranges: propertyRangs)
            }catch { }
            modelTextView.textStorage?.setAttributedString(attributedString)
        }else{
            var classRange = content.ranges(of: "@interface ") + content.ranges(of: "@implementation ") + content.ranges(of: "@end") + content.ranges(of: " id ")
            do {
                let propertyRangs = try viewModel.obtainOCPropertyNameRang(content)
                classRange += propertyRangs
            }catch { }
            attributedString.addAttribute(.foregroundColor, value: "D059D1".color(), ranges: classRange)
            let importRange = content.ranges(of: "#import <Foundation/Foundation.h>")
            attributedString.addAttribute(.foregroundColor, value: "A1B200".color(), ranges: importRange)
            let foundationRange = content.ranges(of: "NSString ") + content.ranges(of: "NSInteger ") + content.ranges(of: "NSArray") + content.ranges(of: "NSNumber") + content.ranges(of: "double") + content.ranges(of: "NSObject")
            attributedString.addAttribute(.foregroundColor, value: "259027".color(), ranges: foundationRange)
            modelTextView.textStorage?.setAttributedString(attributedString)
        }

    }
    
}


extension JSONToModelVC: NSTextViewDelegate, NSTextFieldDelegate {
    func textDidChange(_ notification: Notification) {
        if let object = notification.object as? NSTextView {
            do {
                try viewModel.convertToDic(object.string)
            }catch { }
        }
        
    }
    
    func controlTextDidChange(_ obj: Notification) {
        if let object = obj.object as? NSTextField {
            viewModel.modelPrefix = object.stringValue
        }
    }
}
